package helixoide;

import helixoide.irc.ConnectionThread;
import helixoide.irc.serverType.unreal32;
import helixoide.hxLog;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class otherMain
{
	unreal32 prot = null;//our protocol handler stuff
	private ServerSocket serv = null;
    private Socket clsock = null;
    private Config serverConfig;
    public otherMain(Config serverConfig)
    {
    	this.serverConfig = serverConfig;
    	hxLog.setLogFilename(serverConfig.getLogFileName());
    }
	public void init()
	{
		
		BufferedReader in = null;
		BufferedWriter out = null;
		hxLog.write("Init", "Initializing SLServ");
		try
    	{
			
			serv = new ServerSocket(this.serverConfig.getPort());
			clsock = serv.accept();
			hxLog.write("Init", "Creating Server Socket");
			
			
			in = new BufferedReader(new InputStreamReader(clsock.getInputStream()));
			out = new BufferedWriter(new OutputStreamWriter(clsock.getOutputStream()));
			
			hxLog.write("Init", "In and Out Streams Created");
			prot = new unreal32(out,this,this.serverConfig);
			Util.startThread(new Thread(new ConnectionThread(in,out,this,prot)), "ConnectionThread.java");
       
    	}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	public void sendRaw(String raw)
	{
		prot.generalOut(raw);
	}
	public void die()
	{
		hxLog.write("Death", "Killing SlServ");
		Util.interruptAll();
		try
		{
			serv.close();
			clsock.close();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		System.exit(1);
	}
}	
